package com.peng.leetcode.list;

import java.util.HashMap;
import java.util.Map;

/**
 * DeleteDuplicates1
 * <p>
 * Created on 2022/12/26
 *
 * @author lupeng
 */
public class DeleteDuplicates1 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        Map<Integer, Integer> val2CountMap = new HashMap<>();
        ListNode curr = head;
        while (curr != null) {
            val2CountMap.put(curr.val, val2CountMap.getOrDefault(curr.val, 0) + 1);
            curr = curr.next;
        }

        ListNode sentry = new ListNode(0);
        ListNode p = sentry;
        curr = head;
        while (curr != null) {
            if (val2CountMap.get(curr.val) <= 1) {
                p.next = curr;
                p = p.next;
            }
            curr = curr.next;
            p.next = null;
        }
        return sentry.next;
    }
}
